package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;


/**
 * Created by Megan on 16/4/12.
 */
public class AttendanceDeviationActivity extends Activity implements View.OnClickListener {

    private ListView mLvDeviation;
    private TextView mTvOk;
    private LinearLayout mLlBack;
    private DeviationAdapter adapter;
    private int selectPosition;
    private String[] deviation = {"0米","100米","300米", "500米", "1000米"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_deviation_layout);
        initViews();
    }

    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_attendance_select_deviation_back);
        mTvOk = (TextView) findViewById(R.id.tv_attendance_select_deviation_ok);
        mLvDeviation = (ListView) findViewById(R.id.lv_attendance_select_deviation_list);

        mLlBack.setOnClickListener(this);
        mTvOk.setOnClickListener(this);
        adapter = new DeviationAdapter();
        mLvDeviation.setAdapter(adapter);
        mLvDeviation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                selectPosition=position;
                adapter.notifyDataSetChanged();
            }
        });
        Intent intent=getIntent();
        if("0".equals(intent.getStringExtra("deviation"))){
            selectPosition=0;
        }else if("100".equals(intent.getStringExtra("deviation"))){
            selectPosition=1;
        }else if("300".equals(intent.getStringExtra("deviation"))){
            selectPosition=2;
        }else if("500".equals(intent.getStringExtra("deviation"))){
            selectPosition=3;
        }else if("1000".equals(intent.getStringExtra("deviation"))){
            selectPosition=4;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_attendance_select_deviation_back:
                finish();
                break;
            case R.id.tv_attendance_select_deviation_ok:
                Intent intent =new Intent();
                intent.putExtra("deviation",deviation[selectPosition].split("米")[0]);
                setResult(RESULT_OK,intent);
                finish();
                Log.d("AttendanceDeviationActivity","所选择的偏差范围是：" + deviation[selectPosition]);
                break;
        }
    }



    private class DeviationAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return deviation.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return deviation[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(AttendanceDeviationActivity.this).inflate(R.layout.item_deviation_select,
                        parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(holder);
            holder.text.setText(deviation[position]);
            if(selectPosition==position){
                holder.image.setVisibility(View.VISIBLE);
            }else {
                holder.image.setVisibility(View.GONE);
            }
            return convertView;
        }
    }
    private class ViewHolder {
        public TextView text;
        public ImageView image;

        public ViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.tv_deviation_select_text);
            image = (ImageView) view.findViewById(R.id.iv_deviation_select_img);
        }
    }

}
